//
//  SIViewController.m
//  UI11_test3
//
//  Created by dllo on 16/6/20.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "SIViewController.h"
#import "AddressBook.h"
#import "Contact.h"
#import "SITableViewCell.h"
@interface SIViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation SIViewController

-(void)dealloc{
    [_addressBook release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.addressBook = [[AddressBook alloc] init];
    
    
    [self handleData];
    [self config];
    [self createView];
}
-(void)config{
    self.navigationItem.rightBarButtonItem = [self editButtonItem];
}




-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    [super setEditing:editing animated:animated];
    
    
    [self.tableView setEditing:editing animated:animated];
    
}



-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}


-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSString *key = [[self.addressBook getGroupNames] objectAtIndex:sourceIndexPath.section];
    NSMutableArray *contactArray= [self.addressBook.dicOfContact valueForKey:key];
    
    Contact *con = [contactArray objectAtIndex:sourceIndexPath.row];
    
   
    NSString *toKey = [[self.addressBook getGroupNames] objectAtIndex:destinationIndexPath.section];
    NSMutableArray *toContactArray = [self.addressBook.dicOfContact valueForKey:toKey];

    [con retain];
    
    [contactArray removeObjectAtIndex:sourceIndexPath.row];
    [toContactArray insertObject:con atIndex:destinationIndexPath.row];
   
    if (contactArray.count == 0){
        [self.addressBook.dicOfContact removeObjectForKey:key];
    }
    
    [con release];
    
    [tableView reloadData];
    
}




-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            return UITableViewCellEditingStyleDelete;
            break;
    
        default:
           
            return UITableViewCellEditingStyleInsert;
            break;
    }
    
    
    
}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (editingStyle) {
        case UITableViewCellEditingStyleInsert:
        {
           
            Contact *con = [[Contact alloc] init];
            con.name = @"Tom";
          
            NSString *key = [[self.addressBook getGroupNames] objectAtIndex:indexPath.section];
         
            NSMutableArray *contactArray = [self.addressBook.dicOfContact objectForKey:key];
            [contactArray insertObject:con atIndex:indexPath.row];
 
            [tableView reloadData];
            
        }
            break;
        case UITableViewCellEditingStyleDelete:
        {
            NSString *key = [[self.addressBook getGroupNames] objectAtIndex:indexPath.section];
            NSMutableArray *contactArray = [self.addressBook.dicOfContact objectForKey:key];
           
            [contactArray removeObjectAtIndex:indexPath.row];
           
            if(contactArray.count == 0){
                [self.addressBook.dicOfContact removeObjectForKey:key];
            }
            [tableView reloadData];
        }
            break;
        default:
            break;
    }
    
}


-(void)handleData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Contact" ofType:@"plist"];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    NSArray *keysArray = [dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    for (NSString *key in keysArray){
        NSArray *arrayOfContact = [dictionary valueForKey:key];
        
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
        for (NSDictionary *dic in arrayOfContact){
            Contact *contact = [[Contact alloc] init];
            [contact setValuesForKeysWithDictionary:dic];
            [array addObject:contact];
            [contact release];
        }
        [self.addressBook.dicOfContact setObject:array forKey:key];
    }
    
}

-(void)createView{
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor grayColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100.0;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[SITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [_tableView release];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.addressBook.dicOfContact.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [[self.addressBook getGroupNames] objectAtIndex:section];
    return [[self.addressBook.dicOfContact valueForKey:key] count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [[self.addressBook getGroupNames] objectAtIndex:section];
}

-(NSArray <NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return [self.addressBook getGroupNames];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    NSArray *array = [self.addressBook.dicOfContact valueForKey:[[self.addressBook getGroupNames] objectAtIndex:indexPath.section]];
    Contact *contact = [array objectAtIndex:indexPath.row];
    
//    tableViewCell.textLabel.text = contact.name;
//    
//    UIImage *image = [UIImage imageNamed:contact.header];
//    tableViewCell.imageView.image = image;
    
    tableViewCell.cellContact = contact;
    
    return tableViewCell;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
